package cn.cycleif.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Objects;

/**
 * &#064;Date: 24/05/22-16:45
 * &#064;Description: 用于自动分页，如果有分页信息，通过ThreadLocal传递至其他层
 */
public class PageUtil {
  private static final ThreadLocal<Page<Object>> PAGE_HOLDER = new ThreadLocal<>();

  public static void setCurrentPage(Page<Object> page) {
    PAGE_HOLDER.set(page);
  }

  public static Page<Object> getPage() {
    Page<Object> page = PAGE_HOLDER.get();
    if (Objects.isNull(page)) setCurrentPage(new Page<>());
    return PAGE_HOLDER.get();
  }

  public static Long getPageNo() {
    return getPage().getCurrent();
  }

  public static Long getPageSize() {
    return getPage().getSize();
  }

  public static Long getLimitCurrent() {
    return (getPageNo() - 1) * getPageSize();
  }

  public static void remove() {
    PAGE_HOLDER.remove();
  }

  private PageUtil() {  }
}
